package com.woody.aggregate.domain.contract;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woody.aggregate.infrastructure.contract.entity.ContractRecordEntity;
import com.woody.aggregate.infrastructure.contract.mapper.ContractRecordMapper;
import com.woody.common.web.util.JacksonUtil;
import lombok.Data;

import java.util.List;

/**
 * @author hzy
 * @date 2025/2/21 下午5:22
 */
@Data
public class ContractRecord extends ContractRecordEntity {

    public static ContractRecordMapper mapper;

    static {
        mapper = SpringUtil.getBean(ContractRecordMapper.class);
    }


    public void update(){
        ContractRecordEntity entity = JacksonUtil.reGroup(this, ContractRecordEntity.class);
        mapper.updateById(entity);
    }

    public void create(){
        ContractRecordEntity entity = JacksonUtil.reGroup(this, ContractRecordEntity.class);
        mapper.insert(entity);
    }


    public static List<ContractRecord> findListByCustomerId(Long customerId,Integer customerType){
        List<ContractRecordEntity> entityList = mapper.selectList(Wrappers.lambdaQuery(ContractRecordEntity.class)
                .eq(ContractRecordEntity::getCustomerId, customerId)
                .eq(ContractRecordEntity::getCustomerType, customerType));
        List<ContractRecord> list = JacksonUtil.reGroupArray(entityList, ContractRecord[].class);
        return list;
    }


    public static ContractRecord findByUk(Long customerId,Integer customerType,String type,Float version){
        ContractRecordEntity entity = mapper.selectOne(Wrappers.lambdaQuery(ContractRecordEntity.class)
                .eq(ContractRecordEntity::getCustomerId, customerId)
                .eq(ContractRecordEntity::getCustomerId, customerId)
                .eq(ContractRecordEntity::getType, type)
                .eq(ContractRecordEntity::getVersion, version));
        ContractRecord model = JacksonUtil.reGroup(entity, ContractRecord.class);
        return model;
    }


    public static List<ContractRecord> findListByCustomerIdAndType(Long customerId,String type,Integer customerType){
        List<ContractRecordEntity> entityList = mapper.selectList(Wrappers.lambdaQuery(ContractRecordEntity.class)
                .eq(ContractRecordEntity::getCustomerType, customerType)
                .eq(ContractRecordEntity::getType, type)
                .eq(ContractRecordEntity::getCustomerId, customerId));

        List<ContractRecord> list = JacksonUtil.reGroupArray(entityList, ContractRecord[].class);
        return list;
    }
}
